package com.igeekhome.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.igeekhome.dto.WaybillRuleDto;
import com.igeekhome.mapper.WWaybillRuleMapper;
import com.igeekhome.pojo.WWaybillRule;
import com.igeekhome.pojo.WWaybillRuleExample;
import com.igeekhome.service.IWaybillRulesService;
import com.igeekhome.util.ResultBean;
import com.igeekhome.util.WaybillUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 陈维豪
 */
@Service("waybillRulesService")
public class WaybillRulesServiceImpl implements IWaybillRulesService {

    @Autowired
    private WWaybillRuleMapper wWaybillRuleMapper;

    @Override
    public PageInfo<WaybillRuleDto> selectByAll(Integer page, Integer limit) {

        List<WaybillRuleDto> waybillRuleDtos = wWaybillRuleMapper.listWaybillRuleDtos();
        PageHelper.startPage(page,limit);
        PageInfo<WaybillRuleDto> pageInfo = new PageInfo<>(waybillRuleDtos);
        return pageInfo;
    }

    @Override
    public void saveMethod(WWaybillRule wr) {
        wr.setBeginChar(WaybillUtils.getBeginChar(wr));
        wr.setMaxWayBillId(WaybillUtils.getFirstMaxWayBillId(wr));
        wWaybillRuleMapper.insertSelective(wr);
    }

    @Override
    public void updateMethod(WWaybillRule wr) {
        wWaybillRuleMapper.updateByPrimaryKeySelective(wr);
    }

    @Override
    public ResultBean deleteMethod(String idsStr) {
        if(idsStr.contains(",")){
            idsStr = idsStr.substring(0,idsStr.length()-1);
        }
            int delete = wWaybillRuleMapper.deleteByIdStr(idsStr);
            if(delete > 0){
                return ResultBean.success();
            }else {
                return ResultBean.fail("删除失败");
            }
    }

    @Override
    public void changeUse(WWaybillRule wr) {

        WWaybillRuleExample wWaybillRuleExample = new WWaybillRuleExample();
        wWaybillRuleExample.createCriteria().andTypeIdEqualTo(wr.getTypeId()).andUsageStatusEqualTo("1");
        WWaybillRule wWaybillRule = new WWaybillRule();
        wWaybillRule.setUsageStatus("0");
        wWaybillRuleMapper.updateByExampleSelective(wWaybillRule,wWaybillRuleExample);
        wWaybillRuleMapper.updateByPrimaryKeySelective(wr);
    }

}
